---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[


]]--
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 
    local assets =
    {
        Asset("ANIM", "anim/loramia_item_mechanical_dog_hat.zip"),
    }
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 参数
    local ANIM_SCALE = 1.5
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 检查位置
    local function ground_check(inst)
        local x,y,z = inst.Transform:GetWorldPosition()
        if (TheWorld:HasTag("cave") and not TheWorld.Map:IsAboveGroundAtPoint(x,y,z) ) then
            inst:Remove()
            SpawnPrefab("sinkhole_warn_fx_2").Transform:SetPosition(x,y,z)
        elseif TheWorld.Map:IsOceanAtPoint(x,y,z) then
            inst:Remove()
            SpawnPrefab("splash_sink").Transform:SetPosition(x,y,z)
        end
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- workable
    local function workable_test_fn(inst,doer,right_click)
        return doer.prefab == "loramia" and doer.replica.health:GetCurrent() > 20
    end
    local function custom_workable_install(inst)
        inst:ListenForEvent("Loramia_OnEntityReplicated.loramia_com_workable",function(inst,replica_com)
            replica_com:SetTestFn(workable_test_fn)
            replica_com:SetText("loramia_item_mechanical_dog",STRINGS.ACTIONS.PET)
        end)
        if not TheWorld.ismastersim then
            return
        end
        inst:AddComponent("loramia_com_workable")
        inst.components.loramia_com_workable:SetActiveFn(function(inst,doer)
            if inst:HasTag("working") then
                return false
            end
            inst:AddTag("working")
            inst.AnimState:PlayAnimation("jump")
            inst.AnimState:SetDeltaTimeMultiplier(1.5)
            inst:ForceFacePoint(doer.Transform:GetWorldPosition())
            inst:ListenForEvent("animover",function()
                if not doer:IsValid() or doer:HasTag("playerghost") then
                    local x,y,z = doer.Transform:GetWorldPosition()
                    SpawnPrefab(inst.prefab).Transform:SetPosition(x,y,z)
                    return
                end
                local old_hat = doer.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)
                if old_hat then
                    doer.components.inventory:SetActiveItem(old_hat)
                    doer.components.inventory:ReturnActiveItem()
                end
                doer.components.inventory:Equip(SpawnPrefab("loramia_item_mechanical_dog_hat"))
                doer.components.health:DoDelta(-20)
                inst:Remove()
            end)
            return true
        end)
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- hammer workable
    local function on_work_callback(inst, worker,workleft, numworks)
        if worker and worker:HasTag("player") then
            return
        end
        inst.components.workable:SetWorkLeft(5)
    end
    local function on_finish_callback(inst,worker)
        local fx = SpawnPrefab("collapse_big")
        fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
        inst:Remove()
    end
    local function hammer_workable_install(inst)
        inst:AddComponent("workable")
        inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
        inst.components.workable:SetWorkLeft(5)
        inst.components.workable:SetOnWorkCallback(on_work_callback)
        inst.components.workable:SetOnFinishCallback(on_finish_callback)
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- building
    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddNetwork()

        inst.entity:AddMiniMapEntity()
        inst.MiniMapEntity:SetIcon("loramia_item_mechanical_dog_hat.tex")
        inst.MiniMapEntity:SetPriority(1)

        inst.AnimState:SetBank("loramia_item_mechanical_dog_hat")
        inst.AnimState:SetBuild("loramia_item_mechanical_dog_hat")
        inst.AnimState:PlayAnimation("idle")
        inst.AnimState:SetScale(ANIM_SCALE,ANIM_SCALE,ANIM_SCALE)

        inst.Transform:SetTwoFaced()
        inst.entity:SetPristine()
        ---------------------------------------------------------------------
        --- 
            custom_workable_install(inst)
        ---------------------------------------------------------------------
        if not TheWorld.ismastersim then
            return inst
        end
        ---------------------------------------------------------------------
        ----
            inst:AddComponent("inspectable")
            MakeHauntableLaunch(inst)
        ---------------------------------------------------------------------
        ---- 
            inst:DoTaskInTime(0,ground_check)
        ---------------------------------------------------------------------
        ---- 可拆除 
            hammer_workable_install(inst)
        ---------------------------------------------------------------------
        return inst
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----
    local function placer_postinit_fn(inst)
        inst.AnimState:SetScale(-ANIM_SCALE,ANIM_SCALE,ANIM_SCALE)
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
return Prefab("loramia_item_mechanical_dog", fn, assets),
    MakePlacer("loramia_item_mechanical_dog_placer", "loramia_item_mechanical_dog_hat", "loramia_item_mechanical_dog_hat", "idle", nil, nil, nil, nil, nil, nil, placer_postinit_fn, nil, nil)

